package mesweb.mesweb.controller.procedure;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.Procedure.MesProductProcedure;
import mesweb.mesweb.service.procedure.IMesProductProcedureService;
import mesweb.mesweb.vo.procecdure.MesProductProcedureVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

/**
* @Description: 产品工序
* @Author: jeecg-boot
* @Date:   2021-06-26
* @Version: V1.0
*/

@Api(tags="产品工序")
@RestController
@RequestMapping("/mes/mesProductProcedure")
public class MesProductProcedureController {
   @Autowired
   private IMesProductProcedureService mesProductProcedureService;

   @GetMapping("/GetAll")
   @ApiOperation("获取全部数据列表")
   public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
       List<MesProductProcedureVo> data;
       QueryWrapper<MesProductProcedureVo> queryWrapper = new QueryWrapper<>();
       if (StringUtils.isBlank(name) || name.equals("全部")) {
       }
       data = mesProductProcedureService.getProductProcedure(queryWrapper);
       return CommonResult.success(data);
   }
   /**
    * 分页列表查询
    *
    * @param
    * @param page
    * @param size
    * @param req
    * @return
    */
   @ApiOperation(value="产品工序-分页列表查询", notes="产品工序-分页列表查询")
   @GetMapping(value = "/list")
   public CommonResult<?> queryPageList(
                                  @RequestParam(name="no", required = false, defaultValue="") String productProcedureNo,
                                  @RequestParam(name="updateStartTime", required = false,defaultValue="") Timestamp updateStartTime,
                                  @RequestParam(name="updateEndTime",required = false, defaultValue="") Timestamp updateEndTime,
                                  @RequestParam(name="status",required = false,defaultValue= "-1") Integer status,
                                  @RequestParam(name="page",required = false ,defaultValue="1") Integer page,
                                  @RequestParam(name="size", required = false ,defaultValue="10") Integer size,
                                  HttpServletRequest req) {
       QueryWrapper<MesProductProcedureVo> queryWrapper = new QueryWrapper<>();
       if(productProcedureNo != null && productProcedureNo.length()>0)
       {
           queryWrapper.eq("a.Product_Procedure_No",productProcedureNo);
       }
       else {
           if (updateStartTime != null && updateEndTime != null) {
               queryWrapper.between("b.updateTime", updateStartTime, updateEndTime);
           }
           if (status != -1) {
               queryWrapper.eq("a.status", status);
           }
       }
       Page<MesProductProcedureVo> pageMesProductProcedureVo = new Page<MesProductProcedureVo>(page, size);
       IPage<MesProductProcedureVo> pageList = mesProductProcedureService.getProductProcedure(pageMesProductProcedureVo, queryWrapper);
       return CommonResult.success(pageList);
   }
   /**
    * 编辑
    *
    * @param mesProductProcedure
    * @return
    */
   @ApiOperation(value="产品工序-编辑", notes="产品工序-编辑")
   @PostMapping(value = "/save")
   @Transactional
   public CommonResult<?> edit(@RequestBody MesProductProcedure mesProductProcedure) {
       boolean result = false;
       if(mesProductProcedure.getId() == 0L) {
           QueryWrapper<MesProductProcedure> queryWrapper = new QueryWrapper<>();
           queryWrapper.eq("Produce_Procedure_No", mesProductProcedure.getProductProcedureNo());
           queryWrapper.or().eq("Produce_Procedure_Name", mesProductProcedure.getProductProcedureName());
           MesProductProcedure mesTechnologyFlow1 = mesProductProcedureService.getOne(queryWrapper);
           if (mesTechnologyFlow1 != null) {
               String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getProductProcedureNo(), mesTechnologyFlow1.getProductProcedureName());
               throw new ApiException(msg);
           }
            result = mesProductProcedureService.save(mesProductProcedure);
            //更新瓶颈工序的id,名称和标准工时
            mesProductProcedureService.updateBottleneckProcedure(mesProductProcedure.getId());
       }
       else
       {
           result = mesProductProcedureService.updateById(mesProductProcedure);
       }
       return CommonResult.success(mesProductProcedure);
   }

    /**
    * 通过id删除
    *
    * @param id
    * @return
    */
   @ApiOperation(value="产品工序-通过id删除", notes="产品工序-通过id删除")
   @DeleteMapping(value = "/delete")
   public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
       mesProductProcedureService.removeById(id);
       return CommonResult.success("删除成功!");
   }

   /**
    * 批量删除
    *
    * @param ids
    * @return
    */
   @ApiOperation(value="产品工序-批量删除", notes="产品工序-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public CommonResult<?> deleteBatch(@ApiParam("MesProductProcedureIDs") @RequestBody List<Long> ids) {
       this.mesProductProcedureService.removeByIds(ids);
       return CommonResult.success("批量删除成功！");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @ApiOperation(value="产品工序-通过id查询", notes="产品工序-通过id查询")
   @GetMapping(value = "/queryById")
   public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
       MesProductProcedure mesProductProcedure = mesProductProcedureService.getById(id);
       return CommonResult.success(mesProductProcedure);
   }
}
